<?php
/*
 *  Maniter: A webbased business managment tool.
 *  Copyright (C) 2007  Dimitri Michaux <dimitri.michaux@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * appointment.class.php, Maniter
 * 
 * This class has everything to handle appointments. Save,
 * edit, delete or update appointments. An appointment
 * has a description, subject and a timestamp
 * (derived from DBObject).
 *
 * @author Dimitri Michaux
 * @version 0.1
 * @package maniter_classes
 */

require_once( _PATH_TO_JIBA.'dbobject.class.php' );
require_once( _PATH_TO_JIBA.'timestamp.class.php' );

class Appointment extends DBObject {
  
  private $_subject;
  
  private $_description;
  
  /**
   * Get all the apointments from a given date
   * and limit them amount by $amount
   */
  static public function getByLimit( $amount=10, $today=0 )
  {
    if( $today==0 )
      $today = date( "Y-m-d H:i:s" );
    if( $amount <= 0 )
      $amount = 10;
    
    $db = DB::get();
    
    $condition = "date>='".mysql_real_escape_string($today)."'";
    
    $rows = $db->getRows( 'appointments', $condition, 'date', true, $amount );
    $num = count( $rows );
    $appointments = array();
    
    for( $i=0 ; $i<$num ; $i++ )
    {
      $appointments[$i] = new Appointment;
      $appointments[$i]->_id = $rows[$i]['id'];
      $appointments[$i]->getTimestamp()->setMysqlTimestamp( $rows[$i]['date'] );
      $appointments[$i]->setSubject( $rows[$i]['subject'] );
      $appointments[$i]->setDescription( $rows[$i]['description'] );
    }
    
    return $appointments;
  }
  
  /**
   * Gets all the appointments for one day
   *
   */
  static public function getByDay( $month, $day, $year )
  {
    //startime
    $begin = date( "Y-m-d H:i:s", mktime(0,0,0,$month,$day,$year) );
    //endtime = this month +1 month
    $end   = date( "Y-m-d H:i:s", strtotime('+1 day -1 second',mktime(0,0,0,$month,$day,$year)) );
    
    return Appointment::getByInt( $begin, $end );
  }
  
  /**
   * Get all the appointments for a month
   */
  static public function getByMonth( $month, $year )
  {
    //startime
    $begin = date( "Y-m-d H:i:s", mktime(0,0,0,$month,1,$year) );
    //endtime = this month +1 month
    $end   = date( "Y-m-d H:i:s", strtotime('+1 month -1 day',mktime(0,0,0,$month,1,$year)) );
    
    return $this->getByInt( $begin, $end );
  }
  
  /**
   * Get the appointments by interval
   *
   * Gets all the appointments between and including the start and end value
   */
  static public function getByInt( $start, $end )
  {
    $db = DB::get();  //it appears like on some servers the database should be initialized before
                      //using mysql_real_escape_string, I don't know why this is
    $condition = "date>='".mysql_real_escape_string($start)."' AND date<='".mysql_real_escape_string($end)."'";
    $rows = $db->getRows( 'appointments', $condition, 'date' );
    $num = count( $rows );
    $appointments = array();
    
    for( $i=0 ; $i<$num ; $i++ )
    {
      $appointments[$i] = new Appointment;
      $appointments[$i]->_id = $rows[$i]['id'];
      $appointments[$i]->getTimestamp()->setMysqlTimestamp( $rows[$i]['date'] );
      $appointments[$i]->setSubject( $rows[$i]['subject'] );
      $appointments[$i]->setDescription( $rows[$i]['description'] );
    }
    
    return $appointments;
  }

  static public function getById( $id )
  {
    $db = DB::get();
    $condition = "id='".mysql_real_escape_string($id)."'";
    $res = $db->getRows( 'appointments', $condition );
    
    $app = new Appointment;
    $app->_id = $id;
    $app->getTimestamp()->setMysqlTimestamp( $res[0]['date'] );
    $app->setSubject( $res[0]['subject'] );
    $app->setDescription( $res[0]['description'] );
    
    return $app;
  }

  /**
   * Prints the calendar and highlights all the appointments
   */
  static public function getCalendar( $month=0, $year=0, $amount=3 )
  {
    if( $year == 0 )
      $year = date("Y");
    if( $month == 0 )
      $month = date("n");
    if( $amount <= 0 )
      $amount = 1;
  
    $calendar = new Calendar;
    
    $apps = Appointment::getByInt( date("Y-m-d H:i:s",strtotime("-1 month",mktime(0,0,0,$month,1,$year)) ),
                             date("Y-m-d H:i:s",strtotime("+2 months -1 day",mktime(0,0,0,$month,1,$year))) );

    foreach( $apps as &$app )
      $calendar->addEvent(  $app->getTimestamp()->getMonth(),
                            $app->getTimestamp()->getDay(),
                            $app->getTimestamp()->getYear(),
                            $app->getSubject(),
                            $app->getDescription() );
    
    return $calendar->printCalendars( $month, $year, $amount );
  }

  /**
   * Save an appointment
   */
  static public function save( $app )
  {
    $db = DB::get();
    $cond = "id='".mysql_real_escape_string($app->getId())."'";
    $data = array(  'id'          => $app->getId(),
                    'date'        => $app->getTimestamp()->getMysqlTimestamp(),
                    'subject'     => $app->getSubject(),
                    'description' => $app->getDescription() );
    
    $db->save( 'appointments', $data, true );
  }
  
  /**
   * Create a new appointment
   */
  static public function create()
  {
    $app = new Appointment;
    $app->_id = $app->_createId();
    $app->_stamp = new Timestamp;
    $app->_subject = "";
    $app->_description = "";
    
    return $app;
  }
  
  public function __construct( $id=null )
  {
    parent::__construct();
  }
  
  public function setSubject( $str )
  {
    $this->_subject = $str;
  }
  
  public function setDescription( $str )
  {
    $this->_description = $str;
  }
  
  public function getId()
  {
    return $this->_id;
  }
  
  public function getSubject()
  {
    return $this->_subject;
  }
  
  public function getDescription()
  {
    return $this->_description;
  }
}
?>